import { defineStore } from 'pinia'
import { ref } from 'vue'

export const historySeach = defineStore(
  'historySeach',
  () => {
    const historySeachs = ref<string[]>([])
    const addhistorySeach = (value: string) => {
      if (!historySeachs.value.includes(value)) {
        if (historySeachs.value.length >= 12) {
          historySeachs.value.splice(0, 1)
          historySeachs.value.push(value)
        } else {
          historySeachs.value.push(value)
        }
      }
    }
    const clearAllhistorySeach = () => {
      historySeachs.value = []
    }
    const removehistorySeach = (index: number) => {
      historySeachs.value.splice(index, 1)
    }
    return {
      addhistorySeach,
      clearAllhistorySeach,
      removehistorySeach,
      historySeachs,
    }
  },
  {
    persist: {
      storage: {
        setItem(key, value) {
          return uni.setStorageSync(key, value)
        },
        getItem(key) {
          return uni.getStorageSync(key)
        },
      },
    },
  },
)
